package com.infonautics.action;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.hibernate.Article;
import com.hibernate.Company;
import com.hibernate.Userinfo;
import com.infonautics.dao.IArticleDao;
import com.infonautics.dao.ICompanyDao;
import com.infonautics.dao.imp.IArticleDaoImp;
import com.infonautics.dao.imp.ICompanyDaoImp;
import com.infonautics.page.PageBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.system.dao.ISystemDao;

public class ArticleAction extends ActionSupport implements Preparable,
		SessionAware {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> session;
	private String pageUrl = "/WEB-INF/article/";
	private String successMsg;
	private String url;
	private String errormessage;
	private Userinfo userinfo;
	private ISystemDao<Article> articleDao = new IArticleDaoImp();
	private IArticleDao artDao = new IArticleDaoImp();
	private Article article;
	private PageBean<Article> pageBean;
	private int page = 1;
	private int id;
	private int classid;
	private String title;

	

	

	@Override
	public void prepare() throws Exception {
		if (session.containsKey("userinfo")) {
			if (session.get("userinfo") != null) {
				userinfo = (Userinfo) session.get("userinfo");
			}
		}

	}

	// 查询分公司列表
	public String list() throws Exception {
		String queryString = "";
		if(classid != 0){
			queryString += "and classid = " + classid;
		}
		pageBean = artDao.doQueryList(20, page ,queryString);
		pageBean.setQueryString(queryString);
		return "input";
	}

	public String query() throws Exception{
		String queryString = "";
		if(title !=null && !title.equals("")){
			title=new String(title.getBytes("ISO8859-1"),"UTF-8");
			queryString += "and title like '%" + title + "%'";
		}
		
		
		if(classid != 0){
			queryString += "and classid = " + classid;
		}
		pageBean = artDao.doQueryList(20, page ,queryString);
		pageBean.setQueryString(queryString);
		return "list";
	}

	// 新增公司信息页面
	public String addPage() throws Exception {
		return "input";
	}

	// 新增公司信息
	public String add() throws Exception {
		articleDao.doAdd(article);
		doMsgHandle("添加成功", "Article_list.action?classid="+article.getCategory().getId());
		return ArticleAction.SUCCESS;
	}

	// 修改分公司页面
	public String modifyPage() throws Exception {
		article = articleDao.doOne(id);
		return "input";
	}

	// 修改分公司信息
	public String modify() throws Exception {
		articleDao.doModfiy(article);
		doMsgHandle("修改成功", "Article_list.action?classid="+article.getCategory().getId());
		return ArticleAction.SUCCESS;

	}

	// 删除分公司
	public String delete() throws Exception {
		articleDao.doDelete(id);
		doMsgHandle("删除成功", "Article_list.action?classid="+classid);
		return ArticleAction.SUCCESS;
	}

	// 服务方法
	private void doMsgHandle(String successMsg, String url) {
		this.successMsg = successMsg;
		this.url = url;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getErrormessage() {
		return errormessage;
	}

	public void setErrormessage(String errormessage) {
		this.errormessage = errormessage;
	}

	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public Userinfo getUserinfo() {
		return userinfo;
	}

	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}

	public String getSuccessMsg() {
		return successMsg;
	}

	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public PageBean<Article> getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean<Article> pageBean) {
		this.pageBean = pageBean;
	}

	public int getClassid() {
		return classid;
	}

	public void setClassid(int classid) {
		this.classid = classid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}
